package com.ln.controller.impl;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.util.WebUtils;

import com.ln.common.MD5;
import com.ln.controller.BaseController;
import com.ln.pojo.impl.UserPOJO;
import com.ln.service.impl.UserService;

@Controller
public class RegController extends BaseController {
	@Autowired
	private UserService userService;
	
	@RequestMapping(value = "/regsub", method = RequestMethod.POST)
	public String index(HttpServletRequest request, UserPOJO userPOJO) {
		
		if (userService.isExistEmail(userPOJO.getEmail()) > 0) {
			Map<String, String> msgMap = new HashMap<>();
			msgMap.put("text", "用户已经存在！");
			msgMap.put("jumpURL", "/reg.html");
			msgMap.put("jumpURLtitle", "注册页");
			WebUtils.setSessionAttribute(request, "msg", msgMap);
			return "redirect:/error/msg";
		}
		
		System.out.println("RegController.index()");
		
		userPOJO.setPassword(MD5.md5(userPOJO.getPassword()));
		userService.insert(userPOJO);
		return "redirect:/login";
	}
}